home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2139 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  108 lines

  1. Path: uni-erlangen.de!winx03!sunshine!schoof
  2. From: schoof@informatik.uni-wuerzburg.de (Jochen Schoof)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with c code, please help!
  5. Date: 19 Jan 1996 11:52:27 GMT
  6. Organization: University of Wuerzburg, Germany
  7. Message-ID: <4do0lr$f5n@winx03.informatik.uni-wuerzburg.de>
  8. References: <surgsw-1901960148530001@128.206.206.86>
  9. NNTP-Posting-Host: wi2x01.informatik.uni-wuerzburg.de
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Joel Weinstein (surgsw@mizzou1.missouri.edu) wrote:
  13. : I have been trying to get this very simple piece of code to work for
  14. : hours.  What is the problem???????
  15.  
  16. : #include <stdio.h>
  17.  
  18. : main()
  19. : {
  20. :    int i=0;
  21. :    char  word[100], c;
  22. :    printf("Enter a word:   ");
  23. :    while( (c = getchar()) != '\n')  {
  24. :       *word = c;
  25. :       word == word + 1;
  26. :    }  
  27. :    printf("You entered:  ");  
  28. :    *word = 0;
  29. :    while( *word != '\n' )   
  30. :    {  
  31. :       putchar( *word );
  32. :       word == word + 1; 
  33. :    }
  34. : }  
  35.  
  36. : I think the problem I am having is due to my not knowing when to use the
  37. : '*' operator.  When do you use it?  Also, why won't my goddam compiler let
  38. : me do word++; instead of word == word +1;   I also tried to do *word++;
  39. : and it didn't work, what is the deal with that.  It won't let me put word
  40. : = word + 1;.  It insists on the double ='s.  Why is that?
  41.  
  42. word is the base of an array you defined and therefore cannot be changed,
  43. because you would lose the array in that case. What you need is a special
  44. pointer to move through your char array. word == word + 1; works but does
  45. not do what you think it does. It only compares word to word+1, which is
  46. always false, and throws away this result. The compiler did definetly not
  47. insist on the comparison, but it was your idea, because you did not get
  48. some of C's basic concepts :-) 
  49. Using the * is relatively simple: Whenever you access the object a pointer
  50. points to, use * to dereference it. Changing the pointer itself, e.g. when
  51. moving through an array you need no *. In your program *word is completely
  52. equivalent to word[0] and with C not letting you change the value of word,
  53. it will never mean something different. With my additions wp is a pointer
  54. to char and *wp is a char variable, but not a specific one. I hope you got
  55. the idea. Not being a native speaker it's not too easy to explain it.
  56.  
  57. Here is a solution with minimal changes:
  58.  
  59. #include <stdio.h>
  60.  
  61. int main(void)
  62. {
  63.    char  word[100], *wp=word, c;         /* I added wp as described above */
  64.  
  65.    printf("Enter a word:   ");
  66.    while( (c = getchar()) != '\n')  {
  67.       *wp = c;
  68.       wp++;
  69.    }  
  70.  
  71.    /* Now you've read the newline character, but haven't copied it */
  72.  
  73.    *wp='\n';                    /* now the newline is copied to the array */
  74.    printf("You entered:  ");  
  75.    wp = word;                   /* re-initialize the pointer*/
  76.    while( *wp != '\n' )   
  77.    {  
  78.       putchar( *wp );
  79.       wp++;
  80.    }
  81.    return 0;
  82. }  
  83.  
  84.  
  85. Let me add, that different techniques should be used for reading and
  86. writing words. It might be a good idea to take a look at the functions
  87. scanf() and printf() for this purpose. Even when working with strings
  88. C offer nice functions in its standard library.
  89.  
  90. : ps: is there a way to find out what exactly error messages mean?  I kept
  91. : getting something similar to: not an Ivalue.  It would be nice if I knew
  92. : what the hell that meant.
  93.  
  94. A good reference manual should explain its diagnostics. If it doesn't
  95. I'd get another compiler.
  96.  
  97. - Jochen
  98.  
  99. --
  100. --------------------------------------------------------------------------
  101.  Jochen Schoof                  mailto:schoof@informatik.uni-wuerzburg.de
  102.  Lehrstuhl fuer Informatik II +-------------------------------------------
  103.  Universitaet Wuerzburg       | You are just reading a .sig-light:
  104.  D-97074 Wuerzburg (Germany)  | It is free of fat, sugar and cholesterol!
  105. ------------------------------+-------------------------------------------
  106.  WWW-Homepage:        http://www.informatik.uni-wuerzburg.de/staff/joscho
  107. --------------------------------------------------------------------------
  108.